package net.weamor.ichat.cim.handler;


import java.io.UnsupportedEncodingException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.weamor.ichat.common.util.ContextHolder;

import com.farsunset.cim.nio.constant.CIMConstant;
import com.farsunset.cim.nio.handler.CIMRequestHandler;
import com.farsunset.cim.nio.mutual.ReplyBody;
import com.farsunset.cim.nio.mutual.SentBody;
import com.farsunset.cim.nio.session.CIMSession;
import com.farsunset.cim.nio.session.DefaultSessionManager;
import com.farsunset.cim.nio.util.Base64Coder;

/**
 * 接受客户端发送的消息处理类
 * 
 * @author weamor@163.com
 * 
 */
//发送key client_sendMsg
public class ClientMsgReceive implements CIMRequestHandler {
	private Logger logger = LoggerFactory.getLogger(ClientMsgReceive.class);
	
	@Override
	public ReplyBody process(CIMSession newSession, SentBody message) {
		ReplyBody reply = new ReplyBody();
		DefaultSessionManager sessionManager = ((DefaultSessionManager) ContextHolder
				.getBean("defaultSessionManager"));

		// 账号信息
		String account = message.get("account");

		// 消息内容
		String content = message.get("content");
		try {
			content = new String(Base64Coder.decode(content),"UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//这里需要自定处理数据存储问题
		logger.info(account + "客户端发送的消息内容为:"+content);
		System.out.println(account + "客户端发送的消息内容为:"+content);
		
		//返回成功
		reply.setCode(CIMConstant.ReturnCode.CODE_200);

		return reply;
	}

}
